package com.truthbean.jeetb.utils.web;

import java.io.Writer;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by truth on 16-12-1.
 */
public class Utils {
    public static boolean isEmpty(String s) {
        return s == null || s.trim().equals("");
    }

    public static boolean isEmpty(Object o) {
        return o == null || isEmpty(o.toString());
    }

    public static String getSize(long size, char danwei) {
        if (danwei == 'M') {
            double v = formatNumber(size / 1024.0 / 1024.0, 2);
            if (v > 1024) {
                return getSize(size, 'G');
            } else {
                return v + "M";
            }
        } else if (danwei == 'G') {
            return formatNumber(size / 1024.0 / 1024.0 / 1024.0, 2) + "G";
        } else if (danwei == 'K') {
            double v = formatNumber(size / 1024.0, 2);
            if (v > 1024) {
                return getSize(size, 'M');
            } else {
                return v + "K";
            }
        } else if (danwei == 'B') {
            if (size > 1024) {
                return getSize(size, 'K');
            } else {
                return size + "B";
            }
        }
        return "" + 0 + danwei;
    }

    public static double formatNumber(double value, int l) {
        NumberFormat format = NumberFormat.getInstance();
        format.setMaximumFractionDigits(l);
        format.setGroupingUsed(false);
        return new Double(format.format(value));
    }

    public static boolean isInteger(String v) {
        if (isEmpty(v))
            return false;
        return v.matches("^\\d+$");
    }

    public static String formatDate(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return format.format(new java.util.Date(time));
    }

    public static String convertPath(String path) {
        return path != null ? path.replace("\\", "/") : "";
    }

    public static String htmlEncode(String v) {
        if (isEmpty(v))
            return "";
        return v.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
    }

    public static String getStr(String s) {
        return s == null ? "" : s;
    }

    public static String getStr(Object s) {
        return s == null ? "" : s.toString();
    }

    public static String exec(String regex, String str, int group) {
        Pattern pat = Pattern.compile(regex);
        Matcher m = pat.matcher(str);
        if (m.find())
            return m.group(group);
        return null;
    }

    public static void outMsg(Writer out, String msg) throws Exception {
        outMsg(out, msg, "center");
    }

    public static void outMsg(Writer out, String msg, String align) throws Exception {
        if (msg.indexOf("java.lang.ClassNotFoundException") != -1)
            msg = "Can Not Find The Driver!<br/>" + msg;
        out.write("<div style=\"background:#f1f1f1;border:1px solid #ddd;padding:15px;font:14px;text-align:" + align + ";font-weight:bold;margin:10px\">" + msg + "</div>");
    }
}
